package com.frxuexi.report.view

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.report.R
import com.frxuexi.report.ReportIntent
import com.frxuexi.report.ReportViewModel
import com.frxuexi.report.entity.ReportEntity
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

data class DialogEntity(val type: Int, val content: String, val title: String)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ReportPage(navHostController: NavHostController, vm: ReportViewModel = hiltViewModel()) {
    var list = remember {
        SnapshotStateList<ReportEntity>()
    }

    var isShow by remember {
        mutableStateOf(false)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect {
                when (it) {
                    is IUiState.onSuccess<*> -> {
                        Log.i("===", it.data.toString())
                        list.addAll(it.data as Collection<ReportEntity>)
                    }

                    is IUiState.onError -> {
                        Log.i("===", it.msg)
                    }

                    else -> {}
                }
            }
        }
        vm.sendIntent(ReportIntent.getReports(mapOf("userId" to 1, "page" to 1)))
    })

    Scaffold(floatingActionButton = {
        Image(
            painter = painterResource(id = R.drawable.icon159),
            contentDescription = "",
            Modifier.clickable { isShow = true })
    }) {
        Column(Modifier.padding(it)) {
            MyTitleBar(navHostController = navHostController, title = "工作日志")
            LazyColumn(
                Modifier.padding(horizontal = 20.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp),
                content = {
                    items(list) {
                        ReportItem(entity = it)
                    }
                })
        }
    }

    val dialog_list = listOf<DialogEntity>(
        DialogEntity(1, "1", "日报"),
        DialogEntity(2, "7", "周报"),
        DialogEntity(3, "30", "月报"),
        DialogEntity(4, "季度", "季度报表"),
        DialogEntity(5, "业绩", "业绩报表"),
        DialogEntity(6, "营业", "营业财报")
    )

    if (isShow) {
        AlertDialog(onDismissRequest = { isShow = false }) {
            Column(
                Modifier
                    .fillMaxWidth()
                    .background(Color.White, RoundedCornerShape(20.dp))
                    .padding(20.dp)) {
                Text(text = "新建汇报", fontWeight = FontWeight.Bold)
                Spacer(modifier = Modifier.height(10.dp))
                LazyVerticalGrid(verticalArrangement = Arrangement.spacedBy(10.dp), columns = GridCells.Fixed(3), content = {
                    items(dialog_list) {
                        DialogItem(entity = it){
                            val json = Gson().toJson(it)
                            navHostController.navigate(RouterPath.COMMITREPORT_PAGE+"/$json")
                            isShow=false
                        }
                    }
                })
            }
        }
    }
}

@Composable
fun DialogItem(entity: DialogEntity, onClick: () -> Unit) {
    Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.clickable {
        onClick()

    }) {
        Row(
            Modifier
                .size(60.dp)
                .background(colorResource(id = R.color.lightblue), CircleShape)
                .padding(8.dp)
                .border(
                    1.dp, Color.Blue, RoundedCornerShape(6.dp)
                )
                .background(Color.White, RoundedCornerShape(6.dp))
            ,
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {
            Text(text = entity.content, fontSize = 14.sp)
        }
        Text(text = entity.title, fontSize = 14.sp)
    }
}

@Composable
fun ReportItem(entity: ReportEntity) {
    Column(verticalArrangement = Arrangement.spacedBy(5.dp)) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            Image(
                painter = painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = "",
                Modifier
                    .size(50.dp)
                    .clip(CircleShape)
            )
            val title =
                if (entity.reportType == 1) "日报" else if (entity.reportType == 2) "周报" else if (entity.reportType == 2) "月报"
                else if (entity.reportType == 2) "季度报表" else if (entity.reportType == 2) "业绩报表" else "营业财报"
            Spacer(modifier = Modifier.width(5.dp))
            Text(text = title)
        }
        Text(text = entity.reportNowTitle)
        Text(text = entity.reportNextValue)
        Text(text = entity.reportNextTitle)
        Text(text = entity.reportNextValue)
        val time = SimpleDateFormat("yyyy年MM月dd日").format(entity.reportTime)
        Text(text = time + "发布")
    }
}